package com.zzptc.lzh.myapp;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.zzptc.lzh.myapp.entity.User;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    EditText yhm;
    EditText mm;
    Button dl;
    Button zc;
    Button bc;
    private static final String TAG = "MainActivity";
    List<User> list=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        event();
    }

    public void initView(){
        yhm=findViewById(R.id.yhm);
        mm=findViewById(R.id.mm);
        dl=findViewById(R.id.dl);
        zc=findViewById(R.id.zc);
    }
    public void event(){
        yhm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "请输入用户名", Toast.LENGTH_SHORT).show();
            }
        });
        mm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
            }
        });
        dl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "欢迎回来", Toast.LENGTH_SHORT).show();
                String name = yhm.getText().toString().trim();
                String pwd = mm.getText().toString().trim();
                SharedPreferences sh = getSharedPreferences("user", MODE_PRIVATE);
                sh.getString("username","");


            }
        });
        zc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "正在前往...", Toast.LENGTH_SHORT).show();
                Intent intent=new Intent(MainActivity.this,RegActivity.class);
                startActivityForResult(intent,1);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==1&&resultCode==2){
            //取值
            String name = data.getStringExtra("name");
            String password = data.getStringExtra("password");
            Log.d(TAG, "回传的值是："+name);
            Log.d(TAG, "回传的值是："+password);
            //封装数据
            User user=new User(name,password);
            reg(user);

        }
    }
    public void reg(User user){
        SharedPreferences sp =getSharedPreferences("data",MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();
        editor.putString("username",user.getUsername());
        editor.putString("password",user.getPassword());
        editor.commit();
        //判断有没有对象或者有没有该对象
        /*if (list.size()==0) {
            list.add(user);//要是里面没有对象，直接注册
        }else{
            for (User s:list){
                if (s.getUsername().equals(user.getUsername())){
                    Toast.makeText(this, "该用户已经存在", Toast.LENGTH_SHORT).show();
                    break;//对象存在，结束循环
                }else{
                    list.add(user);//没有该对象，直接注册
                }
            }
        }*/

    }
}
